home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / apport / package-hooks / firefox-3.0.py next >
Encoding:
Python Source  |  2009-09-07  |  6.7 KB  |  144 lines

  1. '''firefox apport hook draft
  2.  
  3. /usr/share/apport/package-hooks/firefox-3.0.py
  4.  
  5. Appends to apport's firefox default report: the files pluginreg.dat and
  6. profiles.ini, and also a summary of all the extensions loaded on each firefox
  7. profile (the summary is the extension's name, it's version, and the id)
  8. obtained by parsing each extension's install.rdf file.
  9.  
  10. Copyright (c) 2007: Hilario J. Montoliu <hmontoliu@gmail.com>
  11.  
  12. This program is free software; you can redistribute it and/or modify it
  13. under the terms of the GNU General Public License as published by the
  14. Free Software Foundation; either version 2 of the License, or (at your
  15. option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
  16. the full text of the license.
  17. '''
  18.  
  19. import os
  20. import ConfigParser
  21. import glob
  22. import cStringIO
  23. from xml.dom import minidom
  24.  
  25. def extensions_ini_parser(extensions_ini_file):
  26.     '''parses profile's extensions.ini file and returns a tuple:
  27.     ((gre extensions, app extensions, local extensions), (gre themes, app themes, local themes))'''
  28.     parser = ConfigParser.ConfigParser()
  29.     parser.read(extensions_ini_file) 
  30.     ext_ini_d = {}
  31.     for section in parser.sections():
  32.         section_gre, section_app, section_local, my_ext = [], [], [], ''
  33.         for extension in parser.options(section):
  34.             my_ext = parser.get(section, extension)
  35.             if '/usr/lib/xulrunner' in my_ext:
  36.                 section_gre.append((my_ext))
  37.             elif '/usr/lib/firefox' in my_ext:
  38.                 section_app.append((my_ext))
  39.             else:
  40.                 section_local.append((my_ext))
  41.         ext_ini_d[section] = (section_gre, section_app, section_local)
  42.     return (ext_ini_d['ExtensionDirs'], ext_ini_d['ThemeDirs'])
  43.  
  44. def install_ini_parser(extension_path):
  45.     '''parses each extension's install.rdf and returns string:
  46.     extension name, its version and the id.'''
  47.     rdf_file = os.path.join(extension_path, 'install.rdf')
  48.     if not os.path.exists(extension_path):
  49.         return '''  %s does not exist (old profile?)''' % extension_path
  50.     refs_dict = {'em:version': '', 'em:id': '', 'em:name': ''}
  51.     parse_err = '%s (Not Parsed)\n' % extension_path 
  52.     dom_doc = minidom.parse(rdf_file)
  53.     for key in refs_dict.keys():
  54.         this_key = ''
  55.         try:
  56.             document_ref = dom_doc.getElementsByTagName('RDF:Description')[0].attributes
  57.             this_key = document_ref[key].value
  58.         except:
  59.             try:
  60.                 document_ref = dom_doc.getElementsByTagName('Description')[0].attributes
  61.                 this_key = document_ref[key].value
  62.             except: 
  63.                 try:
  64.                     this_key = dom_doc.getElementsByTagName(key)[0].childNodes[0].data
  65.                 except:
  66.                     return parse_err
  67.         try: # avoid problems with encodings.
  68.             print >> cStringIO.StringIO(), this_key
  69.             refs_dict[key] = this_key            
  70.         except UnicodeEncodeError:    
  71.             refs_dict[key] = repr(this_key)
  72.     return '''%(em:name)s (version: %(em:version)s) -\tid: %(em:id)s''' % refs_dict
  73.  
  74. def extension_summary_helper(extension_list, section_name, alt_output = 1):
  75.     '''does some output proccessing for extensionSummary'''
  76.     str = ''
  77.     if len(extension_list) > 0:
  78.         str += '''  %s:\n''' % section_name
  79.         for extension in extension_list:
  80.             str += '''    %s\n''' % install_ini_parser(extension)
  81.     else:
  82.         if alt_output == 1: # if 0, don't output anything
  83.             str += '''  No %s in this Profile.\n''' % section_name 
  84.     str += '''\n'''
  85.     return str
  86.  
  87. def add_info(report):
  88.     config_dir = os.path.join(os.environ['HOME'], '.mozilla', 'firefox')
  89.     
  90.     # append pluginreg.dat file:
  91.     pluginreg_dat = os.path.join(config_dir,'pluginreg.dat')
  92.     if os.path.exists(pluginreg_dat):
  93.         report['pluginreg.dat'] = open(pluginreg_dat).read()
  94.     
  95.     # append profiles.ini file & parse it:
  96.     profiles_ini = os.path.join(config_dir,'profiles.ini') 
  97.     if os.path.exists(profiles_ini):
  98.         report['profiles.ini'] = open(profiles_ini).read() 
  99.         # parse profiles.ini: 
  100.         profiles_d = {} # { profile_name : [ profile_path, is_default ] }
  101.         profile_parser = ConfigParser.ConfigParser()
  102.         profile_parser.read(profiles_ini)
  103.         for section in profile_parser.sections():
  104.             if profile_parser.has_option(section, 'Name') and profile_parser.has_option(section, 'Path'):
  105.                 if profile_parser.has_option(section, 'Default'):
  106.                     is_default = profile_parser.get(section, 'Default')
  107.                 else:
  108.                     is_default = 0
  109.                 profiles_d[profile_parser.get(section, 'Name')] = (os.path.join(config_dir, profile_parser.get(section, 'Path')), is_default)
  110.     
  111.     # summarize the extensions loaded on each profile (either global and local):
  112.     extensions_dict, themes_dict, extension_summary = {}, {}, ''
  113.     for profile_name in profiles_d.keys():
  114.         profile_path, is_default = profiles_d[profile_name]
  115.         extensions_ini = os.path.join(profile_path, 'extensions.ini')
  116.         if os.path.exists(extensions_ini):
  117.             # attach each profile's extensions.ini too (not enabled).
  118.             #report['extensions.ini (profile: %s)' % profile_name ] = open(extensions_ini).read()
  119.             (extensions_dict['gre_extensions'], extensions_dict['app_extensions'], extensions_dict['local_extensions']),\
  120.             (themes_dict['gre_theme'], themes_dict['app_theme'], themes_dict['local_theme']) = extensions_ini_parser(extensions_ini)
  121.     
  122.             if is_default == '1': is_default_str = ''' (The Default):'''
  123.             else: is_default_str = ''':'''
  124.             extension_summary += '''Profile "%s"%s\n\n''' % (profile_name, is_default_str)
  125.             extension_summary += extension_summary_helper(extensions_dict['gre_extensions'], 'GRE Extensions')
  126.             extension_summary += extension_summary_helper(extensions_dict['app_extensions'], 'Application Extensions')
  127.             extension_summary += extension_summary_helper(extensions_dict['local_extensions'], 'Local Extensions')
  128.             extension_summary += extension_summary_helper(themes_dict['gre_theme'], 'GRE Theme', 0)
  129.             extension_summary += extension_summary_helper(themes_dict['app_theme'], 'Application Theme', 0)
  130.             extension_summary += extension_summary_helper(themes_dict['local_theme'], 'Local Theme', 0)
  131.         buffer = cStringIO.StringIO() # it's needed for propper apport attachments
  132.         print >> buffer, extension_summary
  133.         buffer.seek(0)
  134.     report['ExtensionSummary'] = buffer.read()
  135.     # debug (comment on production)
  136.     # return report
  137.  
  138. #### debug ####
  139. # (uncomment the 'return report' at add_report())
  140. if __name__ == "__main__":
  141.     D = {}  
  142.     report = add_info(D)
  143.     for key in report.keys(): print '''%s:\n''' % key, report[key]
  144.